package com.ruoyi.back.domain.v3

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.mapper.VideoSinglePriceMapper
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.springframework.stereotype.Service
import java.math.BigDecimal
import javax.persistence.*

@TableName("fly_video_single_price")
@ApiModel("视频单次收费")
@Table(name = "fly_video_single_price")
@Entity
data class VideoSinglePrice(

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.AUTO)
    var id: Long? = null,

    @ApiModelProperty("会员id")
    var memberId: Long? = null,

    @ApiModelProperty("最低多少秒")
    var minSecond: Int? = null,

    @ApiModelProperty("最高多少秒")
    var maxSecond: Int? = null,

    @ApiModelProperty("每增加几秒")
    var addEverySecond: Int? = null,

    @ApiModelProperty("收费多少钱")
    var price: Double? = null,

    @ApiModelProperty("排序")
    var orderNum: Int? = 1
)

@ApiModel("编辑参数")
data class VideoSinglePriceDto(

    @ApiModelProperty("最低多少秒")
    var minSecond: Int? = null,

    @ApiModelProperty("最高多少秒")
    var maxSecond: Int? = null,

    @ApiModelProperty("每增加几秒")
    var addEverySecond: Int? = null,

    @ApiModelProperty("收费多少钱")
    var price: Double? = null,
)

@Service
class VideoSinglePriceService : ServiceImpl<VideoSinglePriceMapper, VideoSinglePrice>() {

    fun appPriceDesc(memberId: Long, duration: Long): String? =
        list(
            KtQueryWrapper(VideoSinglePrice::class.java)
                .eq(VideoSinglePrice::memberId, memberId)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {

                val price = priceCa(memberId, duration) ?: "0.0"
                val lastPrice = getOne(
                    KtQueryWrapper(VideoSinglePrice::class.java)
                        .eq(VideoSinglePrice::memberId, memberId)
                        .orderByDesc(VideoSinglePrice::orderNum)
                        .last("limit 1")
                )
                """
                    ￥${price.toBigDecimal().divide(duration.toBigDecimal(), 2)}/每秒, ${lastPrice?.minSecond ?: 0}秒以上, 每增加${lastPrice?.addEverySecond ?: 0}秒收费增加￥${lastPrice?.price ?: 0}
                """.trimIndent()
            }

    fun priceCa(memberId: Long, duration: Long): String? =
        list(
            KtQueryWrapper(VideoSinglePrice::class.java)
                .eq(VideoSinglePrice::memberId, memberId)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {
                filter {
                    (it.minSecond ?: 0) < duration && duration <= (it.maxSecond ?: 0)
                }.takeIf { it.isNotEmpty() }
                    ?.run {
                        get(0).price.toString()
                    }
                    ?: run {
                        getOne(
                            KtQueryWrapper(VideoSinglePrice::class.java)
                                .eq(VideoSinglePrice::memberId, memberId)
                                .orderByDesc(VideoSinglePrice::orderNum)
                                .last("limit 1")
                        )
                            ?.run {
                                (getOne(
                                    KtQueryWrapper(VideoSinglePrice::class.java)
                                        .eq(VideoSinglePrice::memberId, memberId)
                                        .eq(VideoSinglePrice::orderNum, orderNum!! - 1)
                                        .last("limit 1")
                                ).price?.plus(((duration - minSecond!!) / addEverySecond!!) * price!!))
                                    .toString()
                            }
                    }
            }
}
